package mspdt.pocket.wallet;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class CreateNewPassword extends Activity{
	
	private EditText et_old, et_new, et_confirm;
	private Button btn_done;
	private boolean isHaveOld = false;
	private String new_pswd, confirm_pswd;
	DBAdapter db;
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.create_password);
		db = new DBAdapter(this);
		Bundle bundle = getIntent().getExtras();
		isHaveOld = bundle.getBoolean("isHaveOld");
		et_old = (EditText)findViewById(R.id.create_password_et_old);
		et_new = (EditText)findViewById(R.id.create_password_et_new);
		et_confirm = (EditText)findViewById(R.id.create_password_et_confirm);
		
		et_new.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				new_pswd = et_new.getText().toString();
				if(new_pswd.equals(confirm_pswd))
					btn_done.setClickable(true);
				else
					btn_done.setClickable(false);
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
		});
		
		et_confirm.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				confirm_pswd = et_confirm.getText().toString();
				if(new_pswd.equals(confirm_pswd))
					btn_done.setClickable(true);
				else
					btn_done.setClickable(false);
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
		});
		
		btn_done = (Button)findViewById(R.id.create_password_button_create);
		btn_done.setClickable(false);
		if(!isHaveOld)
			et_old.setVisibility(View.GONE);
		btn_done.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				db.openDataBase();
				if(isHaveOld)
				if(db.getPassword().equals(et_old.getText().toString()))
				{
					db.setPassword(new_pswd, isHaveOld);
					CreateNewPassword.this.finish();
				}
				else
				{
					AlertDialog.Builder wrong_password = new AlertDialog.Builder(CreateNewPassword.this);
					wrong_password.setTitle(getResources().getString(R.string.dialog_wrong_password_title));
					wrong_password.setMessage(getResources().getString(R.string.dialog_wrong_password_message));
					wrong_password.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							
						}
					});
					wrong_password.show();
				}
				else
				{
					db.setPassword(new_pswd, isHaveOld);
					finish();
				}
					
				db.close();
			}
		});
		
	}

}
